package top.wilbert.collegelink.message.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.wilbert.collegelink.common.utils.R;
import top.wilbert.collegelink.message.component.SocketClientDemo;
import top.wilbert.collegelink.message.entity.ChatListEntity;
import top.wilbert.collegelink.message.entity.ChatMessageEntity;
import top.wilbert.collegelink.message.entity.ChatUserLinkEntity;
import top.wilbert.collegelink.message.service.ChatListService;
import top.wilbert.collegelink.message.service.ChatMessageService;
import top.wilbert.collegelink.message.service.ChatUserLinkService;

import java.util.List;

/**
 * @Description: websocket类
 * @Author:Jiiangyu
 * @CreatTime:2023-02-27-18:51
 */
@RestController
@RequestMapping("message/clientSocket")
public class ChatController {

    //    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ChatUserLinkService chatUserLinkService;

    @Autowired
    private ChatMessageService chatMessageService;

    @Autowired
    private ChatListService chatListService;

    @RequestMapping("/openWindow")
    public R openWindow(@RequestParam("fromName") String fromName, @RequestParam("toName") String toName) {
        ChatUserLinkEntity chatUserLinkFromEntity = chatUserLinkService.getLinkByNames(fromName, toName);
        ChatUserLinkEntity chatUserLinkToEntity = chatUserLinkService.getLinkByNames(toName, fromName);
        //获取双方发送的消息
        Long FromLinkId = chatUserLinkFromEntity.getLinkId();
        Long ToLinkId = chatUserLinkToEntity.getLinkId();

        //更新chat_list表 from_window , unread清零
        chatListService.updateListByLinkId(FromLinkId);

        List<ChatMessageEntity> list = chatMessageService.getByLinkId(FromLinkId, ToLinkId);

        return new R().put("data",list);
    }

    /**
     * 进入聊天消息模块  根据fromName查询已存在(status = 1)的聊天列表
     *
     * @param fromName
     * @return top.wilbert.collegelink.common.utils.R
     **/
    @RequestMapping("/getChatList")
    public R getChatList(@RequestParam("fromName") String fromName) {
        List<ChatListEntity> list = chatListService.getChatListByFromUser(fromName);

        return new R().put("data",list);
    }

    @RequestMapping("/sendMsg/{chat}/{msg}")
    public void sendMsg(@PathVariable("chat") String chat, @PathVariable("msg") String msg) {
        if ("group".equals(chat.toLowerCase())) {
            //群聊
            SocketClientDemo.sendMsg("{\"type\":\"MESSAGE\",\"chat\":\"GROUP\",\"from\":\""+SocketClientDemo.getFromUserId()+"\",\"to\":\"群号：xxxx\",\"message\":\"" + msg + "\"}");
        } else {
            //私聊
            SocketClientDemo.sendMsg("{\"type\":\"MESSAGE\",\"chat\":\"PRIVATE\",\"from\":\""+SocketClientDemo.getFromUserId()+"\",\"to\":\"" + chat + "\",\"message\":\"" + msg + "\"}");
        }
    }

    @RequestMapping("/starClient/{userId}")
    public void starClient(@PathVariable("userId") String userId) {
        SocketClientDemo.open(userId);
    }

    @RequestMapping("/closeClient")
    public void closeClient() {
        SocketClientDemo.close();
    }


}
